package com.action.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.action.childbase.ChildBaseFragment;
import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.UserDeposit;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.util.AppConstants;
import com.xinglai.yun.R;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.Collection;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryDepositFragment extends ChildBaseFragment<XYBUPressenter> {
    @BindView(R.id.data_container)
    XRecyclerView dataContainer;


    private DepositLogAdapter depositLogAdapter = null;

    public static class DepositLogAdapter extends RecyclerAdapter<UserDeposit, DepositLogAdapter.ViewHolder> {
        @Override
        protected void fillData(UserDeposit item, ViewHolder holder) {
            holder.depositOrdersn.setText(item.getOrderSn());
            holder.depositDate.setText(item.getCreateAt());
            holder.depositMoney.setText(item.getPriceCount() + "元");
            switch (item.getStatus()) {
                case 0:
                    holder.depositStatu.setText("待处理");
                    holder.result.setVisibility(View.GONE);
                    break;
                case 1:
                    holder.result.setVisibility(View.VISIBLE);
                    holder.depositStatu.setText("已到账");
                    holder.result.setText("到账时间:" + item.getFinishAt());
                    break;
                case 2:
                    holder.result.setVisibility(View.VISIBLE);
                    holder.depositStatu.setText("已经冻结");
                    holder.result.setText("备注:" + item.getMemo());
                    break;
                case 3:
                    holder.result.setVisibility(View.VISIBLE);
                    holder.depositStatu.setText("失败");
                    holder.result.setText("备注:" + item.getMemo());
                    break;
            }
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.unit_deposit_log;
        }

        public class ViewHolder extends RecyclerViewHolder {
            @BindView(R.id.deposit_ordersn)
            TextView depositOrdersn;
            @BindView(R.id.deposit_statu)
            TextView depositStatu;
            @BindView(R.id.content)
            TextView content;
            @BindView(R.id.depositDate)
            TextView depositDate;
            @BindView(R.id.depositMoney)
            TextView depositMoney;
            @BindView(R.id.depositStockCount)
            TextView depositStockCount;
            @BindView(R.id.result)
            TextView result;

            public ViewHolder(View itemView) {
                super(itemView);
            }

        }
    }

    private int pageNO = 1;

    @Override
    protected boolean isNeedShowLoading() {
        return true;
    }

    @Override
    protected boolean needShowError() {
        return true;
    }

    @Override
    protected void loadData() {
        super.loadData();
        if (null != mPresenter)
            mPresenter.fetchHistoryDepositLog(pageNO);
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        depositLogAdapter = new DepositLogAdapter();

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        dataContainer.setLayoutManager(layoutManager);
        dataContainer.setAdapter(depositLogAdapter);
        dataContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (dataContainer == null){
                    return;
                }
                pageNO = 1;
                if (null != mPresenter)
                    mPresenter.fetchHistoryDepositLog(pageNO);
                dataContainer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (dataContainer != null) {
                            dataContainer.refreshComplete();
                        }
                    }
                }, AppConstants.Const.REFRESHTIME);
            }

            @Override
            public void onLoadMore() {
                pageNO++;
                if (null != mPresenter)
                    mPresenter.fetchHistoryDepositLog(pageNO);
                dataContainer.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dataContainer.loadMoreComplete();
                    }
                }, AppConstants.Const.REFRESHTIME);
            }
        });
        dataContainer.setEmptyView(getEmptyView(dataContainer, "没有记录", "", null));
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof Collection) {
            if (UserDeposit.class == tClass) {
                if (depositLogAdapter == null || dataContainer == null) {
                    return;
                }

                if (pageNO == 1)
                    depositLogAdapter.clear();
                depositLogAdapter.addAll((List<UserDeposit>) object);
                depositLogAdapter.notifyDataSetChanged();
                dataContainer.setLoadingMoreEnabled(paginated.getHasMore());

            }
        }
        setState(AppConstants.STATE_SUCCESS);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_history_deposit;
    }

}
